package com.yunkaweilai.android.view.dialog.room;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.RoomPriceInterface;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.MoneyTextWatcher;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 修改房费
 * Created by Administrator on 2016/12/22 0022.
 */

public class RoomUpdatePriceDialog extends Dialog implements View.OnClickListener{
    TextView idImgClose;
    TextView idTvRight;
    EditText idEdtTypeName;
    private Context context;
    private RoomPriceInterface roomPriceInterface;
    private String gid;
    public RoomUpdatePriceDialog(Context context, RoomPriceInterface roomPriceInterface,String gid) {
        super(context);
        this.gid = gid;
        this.context = context;
        this.roomPriceInterface = roomPriceInterface;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update_price);
        initView();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run(){
                InputMethodManager inputManager = (InputMethodManager) idEdtTypeName.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(idEdtTypeName, 0);
            }
        },800);
    }


    private void initView(){
        idImgClose = (TextView) findViewById(R.id.id_tv_close);
        idTvRight = (TextView) findViewById(R.id.id_tv_right);
        idImgClose.setOnClickListener(this);
        idTvRight.setOnClickListener(this);
        idEdtTypeName = (EditText) findViewById(R.id.id_edt_type_name);
        idEdtTypeName.addTextChangedListener(new MoneyTextWatcher(idEdtTypeName));
//        SpannableString ss = new SpannableString("输入改价金额");//定义hint的值
//        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(18,true);//设置字体大小 true表示单位是sp
//        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        idEdtTypeName.setHint(new SpannedString(ss));
        idEdtTypeName.setFocusable(true);
        idEdtTypeName.setFocusableInTouchMode(true);
        idEdtTypeName.requestFocus();
    }


    private void addType(){
        HttpRequest.postUrl(Api.GUADAN_UPDATE_ROOM_PRICE)
                .addParams("gid",gid)
                .addParams("fangfei",idEdtTypeName.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (checkCode){
                            roomPriceInterface.ReturnPrice(idEdtTypeName.getText().toString());
                            MyToast.showShortToast(context,"操作成功");
                            dismiss();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        MyToast.showShortToast(context,"修改房费失败");
                    }
                });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.id_tv_close:
                dismiss();
                break;
            case R.id.id_tv_right:
                if (!StringUtils.isEmpty(idEdtTypeName.getText().toString())){
                    addType();
                }
                dismiss();
                break;
        }
    }
}
